package cn.uexpo.lhz.controller.admin;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.uexpo.lhz.biz.EventLabelBiz;
import cn.uexpo.lhz.common.oldUtils.PageRequest;
import cn.uexpo.lhz.common.oldUtils.PageResult;
import cn.uexpo.lhz.common.utils.JsonData;
import cn.uexpo.lhz.plus.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import cn.uexpo.lhz.common.oldBase.BaseController;
import cn.uexpo.lhz.domain.EventLabel;
import cn.uexpo.lhz.domain.EventLabelExample;

/**
 * 项目标签管理
 *
 * @author
 * @since
 */
@RestController
@RequestMapping("/cert/eventLabel")
public class EventLabelController extends BaseController<EventLabel> {

    @Autowired
    private EventLabelBiz eventLabelService;

    @RequestMapping("/eventLabelAll")
    public Object eventLabelAll(EventLabel eventLabel) {
        EventLabelExample example = new EventLabelExample();
        List<EventLabel> list = eventLabelService.selectByExample(example);

        JsonData jsonData = new JsonData();
        jsonData.setCode(200);
        jsonData.setData(list);
        return jsonData;
    }

    /**
     * @return
     */
    @GetMapping("/findPage")
    public R findPage(@RequestParam Map<String, Object> prams) {
        String page = (String) prams.get("page");
        String limit = (String) prams.get("limit");
        String key = (String) prams.get("key");
        PageRequest pageRequest = new PageRequest();
        PageResult result = null;
        if (!"".equals(Integer.parseInt(page)) && !"".equals(Integer.parseInt(limit))) {
            pageRequest.setPage(Integer.parseInt(page));
            pageRequest.setLimit(Integer.parseInt(limit));
        }

        EventLabelExample eventLabelExample = new EventLabelExample();
        EventLabelExample.Criteria criteria = eventLabelExample.createCriteria();
     if(!StringUtils.isEmpty(key)){
		 criteria.andLabelNameEqualTo(key);
	 }
        eventLabelExample.setOrderByClause("label_id desc");
        result = eventLabelService.findPage(pageRequest, eventLabelExample);
        return R.ok().put("result", result);
    }


    /**
     * 规则新建
     *
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/addLabel")
    public Map<String, Object> addLabel(@RequestBody EventLabel eventLabel)  {
        String code = "500";
        String msg = "失败";
        int no = 0;
        Map<String, Object> map = new HashMap<String, Object>();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String labelName = eventLabel.getLabelName();// 名称
//        String labelCreator = eventLabel.getLabelCreator();
//        String validity = "0";//默认有效
//        String state = "0";//默认正常
        Date createTime = new Date();
        EventLabelExample eventLabelExample = new EventLabelExample();
        EventLabelExample.Criteria criteria = eventLabelExample.createCriteria();

        if (null != eventLabel.getLabelName() && !"".equals(eventLabel.getLabelName())) {
            criteria.andLabelNameEqualTo(eventLabel.getLabelName());
            List<EventLabel> eventLabels = eventLabelService.selectByExample(eventLabelExample);
            if (null != eventLabels && eventLabels.size() > 0) {
                msg = "标签已经存在";
            } else {
                eventLabel.setLabelCreatetime(createTime);
                no = eventLabelService.insert(eventLabel);
            }
        } else {
            msg = "请输入标签名(名称不能有逗号,名称中的逗号会被去除)";
        }

        if (1 == no) {
            code = "200";
            msg = "成功";
        }
        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

}
